<?php

class ResourceDatedList extends ResourceList
{

    protected $dateField;
    protected $iniDia;
    protected $iniMes;
    protected $iniAnho;
    protected $finDia;
    protected $finMes;
    protected $finAnho;

    function __construct( $searchclass, $numperpage, $where, $orders,
            $date_field )
    {
        parent::__construct( $searchclass, $numperpage, $where, $orders );

        $this->dateField = $date_field;
    }

    function setFechaIni( $fechaIni )
    {
        if ( !is_array( $fechaIni ) ) return false;
        foreach ( $fechaIni as $i ) if ( !is_numeric( $i ) ) return false;

        $this->iniDia = $fechaIni[ 0 ];
        $this->iniMes = $fechaIni[ 1 ];
        $this->iniAnho = $fechaIni[ 2 ];
    }

    /*
     * @param string $fechaIni Fecha de inicio
     * @param string $fechaFin Fecha de fin
     */

    function setFechaFin( $fechaFin )
    {
        if ( !is_array( $fechaFin ) ) return false;
        foreach ( $fechaFin as $i ) if ( !is_numeric( $i ) ) return false;

        $this->finDia = $fechaFin[ 0 ];
        $this->finMes = $fechaFin[ 1 ];
        $this->finAnho = $fechaFin[ 2 ];
    }

    function handleGET( Event $e )
    {
        parent::handleGET( $e );

        if ( $e->tag == 'busqueda' ) {

            $this->_setPage( 1 );
            $this->_setNumItems( null );

            if ( !empty( $_GET[ 'fecha_ini' ] ) )
                    $this->_setFechaIni( explode( '/', $_GET[ 'fecha_ini' ] ) );
            else $this->_setFechaIni( null );


            if ( !empty( $_GET[ 'fecha_fin' ] ) )
                    $this->_setFechaFin( explode( '/', $_GET[ 'fecha_fin' ] ) );
            else $this->_setFechaFin( null );

            $e->cancelBubble();
        }
    }

    function setWhere()
    {
        parent::setWhere();

        if ( !empty( $this->iniDia ) ) {

            $fecha = $this->iniAnho . '-' . $this->iniMes . '-' . $this->iniDia . ' 00:00:00';
            $this->andWhere( $this->dateField . '>=' . MyLink::quote( $fecha ) );
        }

        if ( !empty( $this->finDia ) ) {

            $fecha = $this->finAnho . '-' . $this->finMes . '-' . $this->finDia . ' 23:59:59';
            $this->andWhere( $this->dateField . '<=' . MyLink::quote( $fecha ) );
        }
    }

}

?>